#! /usr/bin/env python3

from stp_core.loop.looper import Looper
from plenum.common.signer_simple import SimpleSigner

from sovrin_client.client.client import Client
from sovrin_common.config_util import getConfig


# TODO: This code is obsolete, moreover we have a CLI command for this.


looper = Looper(debug=True)

config = getConfig()
basedirpath = config.baseDir

# Steward that will be used to create a new steward by submitting a
# NYM transaction
# TODO: Make this name configurable
clientName = 'Steward1'

# This is because i know the seed.
# TODO: Make this configurable, maybe read the private key/seed from
# command line
seed = ('0' * (32 - len(clientName)) + clientName).encode()

signer = SimpleSigner(seed=seed)
client_address = ('0.0.0.0', 9760)

client = Client(clientName,
                nodeReg=None,
                ha=client_address,
                signer=signer,
                basedirpath=basedirpath)

looper.add(client)

# give the client time to connect
# TODO: Use looper and `eventually` to check whether request succeeded
looper.runFor(3)

# Steward that will be used to create a new node by submitting a
# NODE transaction. This steward is like the owner of this node.
# TODO: Make this name configurable
name = "Steward5"
# This is the seed i want to use for creating keys for the new steward.
# TODO: Make this configurable, maybe read the private key/seed from
# command line
sseed = ('0' * (32 - len(name)) + name).encode()
verkey = SimpleSigner(seed=sseed).verkey
client.submitNewSteward("Steward5", verkey)
# give the client time to connect
# TODO: Use looper and `eventually` to check whether request succeeded
looper.runFor(3)

# This is the name of the newly created node. This would be received
# from the administrator of the node.
# TODO: Make this name configurable
name = "Node5"
# This is the seed i used creating keys for the new node. This would be
# received from the administrator of the node.
# TODO: Make this configurable, maybe read the private key/seed from
# command line

nseed = ('0' * (32 - len(name)) + name).encode()
nodeverkey = SimpleSigner(seed=nseed).verkey

# Here we would add the ip and port of the node. This would be received
# from the administrator of the node.
nodeStackHa = ("127.0.0.1", 9709)
clientStackHa = ("127.0.0.1", 9710)

signer = SimpleSigner(seed=sseed)
client_address = ('0.0.0.0', 9761)
client = Client("Steward5",
                None,
                ha=client_address,
                signer=signer,
                basedirpath=basedirpath)

looper.add(client)
# TODO: Use looper and `eventually` to check whether request succeeded
looper.runFor(3)

client.submitNewNode(name, nodeverkey, nodeStackHa, clientStackHa)
# TODO: Use looper and `eventually` to check whether request succeeded
looper.runFor(3)